/*
 * @Autor: Shihh
 * @Description: 
 * @Version: 1.0
 * @Date: 2020-05-08 10:14:01
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-06-05 18:02:00
 */
const Sequelize = require('sequelize')
const Op = Sequelize.Op;
var comm = require('../comm')

module.exports = (sequelize, DataTypes) => {

    class m_icon extends Sequelize.Model {


           /**
     * @author: xgq
     * @description: 编辑图标信息
     * @param {type} 
     * @return: 
     */
    static async updateIcon(p) {
        let result = await this.update({
            file_url: p.file_url,
            name: p.name,
            views: p.views,
            downloads: p.downloads,
            designer: p.designer,
        }, { where: { id: p.id } })
        return result
      }

        // 单个图标上下架
        static async update_is_enable(pars) {
            var result = this.update({ is_enable: pars.is_enable }, { where: { id: pars.id } })
            return result;
        }
        // 多个个图标上下架
        static async iconsUpORDown(pars) {
            let result = '';
            result = await this.update({ is_enable: pars.status }, { where: { id: { [Op.in]: pars.ids } } })
            return result;
        }
        // 获取图标列表
        static async getAllIcons(pars) {
            var [where=null]=[pars.where]
            let condition = {
                attributes: ['id', 'name','size','category','tags','file_url','views','downloads','create_time','icon_type'],
                raw: true,
            }
            if(where!==null) condition.where=where
            let querydata = await this.findAndCountAll(condition)
            return querydata
        }
    }
    m_icon.init({
        id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
        name: { type: DataTypes.STRING(255), comment:"图标名称"},
        size: { type: DataTypes.STRING(45), comment:"图标大小"},
        category: { type: DataTypes.INTEGER , comment:"分类ID"},
        tags: { type: DataTypes.INTEGER , tags:"标签的ID"},
        icon_type: { type: DataTypes.INTEGER , comment:"图标类型，0为svg"},
        file_url: { type: DataTypes.STRING(45) , comment:"文件地址"},
        views: { type: DataTypes.INTEGER, comment:"查看次数"},
        downloads: { type: DataTypes.INTEGER, comment:"下载次数"},
        create_time: { type: DataTypes.INTEGER, comment:"创建时间戳10位"},
        is_enable: { type: DataTypes.INTEGER, comment:"0上架，1下架"},
        designer: { type: DataTypes.STRING(128), comment:"图标作者"},
        tagIds: { type: DataTypes.STRING(511), comment:"冗余字段，tagIds"},
    }, {
        sequelize,
        modelName: 'icons',
        freezeTableName: true,
        timestamps: false
    });

    return m_icon;

}